//
//  game.c
//  扫雷
//
//  Created by 卜绎皓 on 2022/1/24.
//

#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)//该函数的作用是为数组初始化，设计展示的界面
{
 int i = 0;
 int j = 0;
 for (i = 0; i < rows; i++)
 {
 for (j = 0; j < cols; j++)
 {
     board[i][j] = set;
 }
 }
}
void DispalyBoard(char board[ROWS][COLS], int row, int col)//打印数组函数
{
 int i = 0;
 int j = 0;
 for ( i = 0; i <=row; i++)
 {
 printf("%-2d", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 printf("%d", i);
 for (j = 1; j <= col; j++)
 {
 printf("%2c", board[i][j]);
 }
 printf("\n");
 }
}
void SetMine(char mine[ROWS][COLS], int row, int col)//布置雷的函数
{
    int count = EASYCOUNT;//难度，可以在头文件中更改;
 while (count)
 {
 int x = rand() % row + 1;
 int y = rand() % col + 1;
 if (mine[x][y]=='0')
 {
 mine[x][y] = '1' ;
 count--;
 }
 }
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)//玩家输入坐标时，判断该坐标有没有雷，如果没有就返回周围一圈的雷数
{
 
 return (mine[x - 1][y] +
 mine[x - 1][y - 1] +
 mine[x][y - 1] +
 mine[x + 1][y - 1] +
 mine[x + 1][y] +
 mine[x][y + 1] +
 mine[x+1][y + 1] +
 mine[x - 1][y + 1] - 8 * '0');
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//玩家排雷
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win-row*col-EASYCOUNT)
 {
 printf("请输入要排查的坐标:>");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= row && y >= 1 && y <= col)
 {
 if (mine[x][y] == '1')
 {
 printf("很不幸，你被炸死了\n");
 DispalyBoard(mine, row, col);
 break;
 }
 else
 {
 int count = GetMineCount(mine,x,y);
 show[x][y] = count + '0';
 DispalyBoard(show, row, col);
 win++;
 }
 if (win == row*col - EASYCOUNT)
 {
 printf("恭喜你通关了!!!\n");
 DispalyBoard(mine, row, col);
 }
 }
 else
 {
 printf("注意输入范围,请重新输入:>\n");
 }
 }
}

